﻿using Mono.Data.Sqlite;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class AnswerMgr : MonoBehaviour {
    public static AnswerMgr s_Instance;
    public List<AnswerExamPrefab> m_ExamList = new List<AnswerExamPrefab>();
    public AnswerExamPrefab m_AnswerExamPrefab;
    public Transform m_Content;
    public Text m_ExamNameText, m_EditorText, m_StemText;
    public Text m_UserText, m_ProcessText;
    public Text m_ScoreText;
    public GameObject m_ChooseExamPart,m_ExamPart;
    public string m_EditorName;

    public AnswerExamPrefab m_AnswerExamPrefabNow;
	// Use this for initialization
	void Awake () {
        s_Instance = this;

    }
    private void Start() {
        m_EditorName = PlayerPrefs.GetString("EditorName", "555");
        m_UserText.text = m_EditorName;
        string cmd = "select * from PAPER_TABLE ORDER BY ID";
        SqliteDataReader reader = SQLManager.s_Instance.ExecuteSQLCommand(cmd);
        if (reader != null) {
            DataTable dataTable = new DataTable();
            dataTable.Load(reader);
            for (int i = 0; i < dataTable.Rows.Count; i++) {
               string name = dataTable.Rows[i]["Name"].ToString();
               string ID = dataTable.Rows[i]["ID"].ToString();
               string editor = dataTable.Rows[i]["Editor"].ToString();
               string stem = dataTable.Rows[i]["Stem"].ToString();
                AnswerExamPrefab answerExam = Instantiate(m_AnswerExamPrefab) as AnswerExamPrefab;
                answerExam.m_NameText.text = name;
                answerExam.m_NumberText.text ="试卷"+ ID+":";
                answerExam.m_Editor = editor;
                answerExam.m_Stem = stem;
                answerExam.transform.SetParent(m_Content);
                answerExam.transform.localScale = new Vector3(1f, 1f, 1f);
                
                m_ExamList.Add(answerExam);
            }
            reader.Close();
        }
    }

    // Update is called once per frame
    void Update () {
		
	}
    public void OnClickStartExam() {
        if(m_AnswerExamPrefabNow == null) {
            print("未选择试卷");
            return;
        }
        string paper_id = m_AnswerExamPrefabNow.m_NumberText.text.Replace("试卷", "").Replace(":", "");
        string cmd = "select * from QUESTION_TABLE where ID in (select Question_ID from PAPER_QUESTION_TABLE where Paper_ID = " + paper_id + ")";
        SqliteDataReader reader = SQLManager.s_Instance.ExecuteSQLCommand(cmd);
        if (reader != null) {
            DataTable dataTable = new DataTable();
            dataTable.Load(reader);
            for (int i = 0; i < dataTable.Rows.Count; i++) {
                int ID = (int)dataTable.Rows[i]["ID"];
                int Type = (int)dataTable.Rows[i]["Type"];
                string stem = dataTable.Rows[i]["Stem"].ToString();
                string Pic_Path = dataTable.Rows[i]["Pic_Path"].ToString();
                AnswerCtl.Question question = new AnswerCtl.Question(ID,Type,stem,Pic_Path);
                AnswerCtl.s_Instance.m_QuestionList.Add(question);
            }
            reader.Close();
        }
        m_ChooseExamPart.SetActive(false);
        m_ExamPart.SetActive(true);
        // CheckLastRecord(paper_id);
        AnswerCtl.s_Instance.ShowQuestionNow();

    }
    void CheckLastRecord(string paper_id) {
        string cmd = "select * from ANSWER_TABLE where PAPER_ID = " + paper_id + " AND " + "USER_ID ='" + m_EditorText.text + "'";
        SqliteDataReader reader = SQLManager.s_Instance.ExecuteSQLCommand(cmd);


        string cmd1 = "select * from PAPER_QUESTION_TABLE where Paper_ID = " + paper_id;
        SqliteDataReader reader1 = SQLManager.s_Instance.ExecuteSQLCommand(cmd1);
        DataTable dataTable1 = new DataTable();
        dataTable1.Load(reader1);

        if (reader != null) {
            DataTable dataTable = new DataTable();
            dataTable.Load(reader);
            int recordCount1 = 0;
            int recordCount2 = 0;
            if (dataTable.Rows.Count > 0) {
                recordCount1 = int.Parse(dataTable.Rows[dataTable.Rows.Count - 1]["QID"].ToString());
                recordCount2 = int.Parse(dataTable1.Rows[dataTable1.Rows.Count - 1]["Question_ID"].ToString());
            }
            if (recordCount1 != recordCount2 || dataTable.Rows.Count < 1) {
                AnswerCtl.s_Instance.m_QuestionIndexNow = recordCount1;
                AnswerCtl.s_Instance.ShowQuestionNow();
            } else {
                //已完成试卷
                int score = 0;
                for(int i =0;i< dataTable.Rows.Count; i++) {
                    score += (int)dataTable.Rows[i]["SCORE"];
                }
                m_ScoreText.text = score.ToString();
                AnswerCtl.s_Instance.OnAnswerEnd();
                print("已完成试卷");
            }
            reader.Close();
        }
    }
    public void OnExamClick(AnswerExamPrefab exam) {
        m_AnswerExamPrefabNow = exam;
        foreach(var item in m_ExamList) {
            item.SetState(false);
        }
        exam.SetState(true);

        m_ExamNameText.text = exam.m_NameText.text;
        m_EditorText.text = exam.m_Editor;
        m_StemText.text = exam.m_Stem;
        //string paper_id = m_AnswerExamPrefabNow.m_NumberText.text;
        //string cmd = "select * from ANSWER_TABLE where PAPER_ID = " + paper_id + " AND " + "USER_ID ='" + m_EditorText.text + "'";
        //SqliteDataReader reader = SQLManager.s_Instance.ExecuteSQLCommand(cmd);


        //string cmd1 = "select * from PAPER_QUESTION_TABLE where Paper_ID = " + paper_id;
        //SqliteDataReader reader1 = SQLManager.s_Instance.ExecuteSQLCommand(cmd1);
        //DataTable dataTable1 = new DataTable();
        //dataTable1.Load(reader1);

        //if (reader != null) {
        //    DataTable dataTable = new DataTable();
        //    dataTable.Load(reader);
        //    int recordCount1 = 0;
        //    int recordCount2 = 0;
        //    if (dataTable.Rows.Count > 0) {
        //        recordCount1 = int.Parse(dataTable.Rows[dataTable.Rows.Count - 1]["QID"].ToString());
        //        recordCount2 = int.Parse(dataTable1.Rows[dataTable1.Rows.Count - 1]["Question_ID"].ToString());
        //    }
        //    if (recordCount1 != recordCount2|| dataTable.Rows.Count<1) {
        //        m_ProcessText.text = "题目" + recordCount1.ToString();
        //    } else {
        //        //已完成试卷
        //        m_ProcessText.text = "试卷已完成";
        //    }
        //    reader.Close();
        //}
    }
    public void OnClickHomeButton() {
        SceneManager.LoadScene("Scene");
    }

}
